home *** CD-ROM | disk | FTP | other *** search
- Path: news.MWCI.NET!usenet
- From: stuffle@pcii.net
- Newsgroups: comp.lang.c++
- Subject: Re: [Q] Missinterpreting *protected* access modifier?
- Date: 1 Feb 1996 02:11:42 GMT
- Organization: MidWest Communications, Inc.
- Message-ID: <4ep7gu$ao1@hihat.mwci.net>
- References: <DLrCGF.G1F@wn.planet.gen.nz>
- Reply-To: stuffle@pcii.net
- NNTP-Posting-Host: lan-pm1-16.pcii.net
- X-Newsreader: IBM NewsReader/2 v1.2.5
-
- In <DLrCGF.G1F@wn.planet.gen.nz>, hugp@kp.planet.gen.nz (Peter Hug) writes:
- >Am I missinterpreting the meaning of the *protected* access modifier?
-
- Sort of.
-
- >The following code does not compile under VC++:
- >
- > ============================================
- > class A
- > {
- > protected:
- > int i;
- > int GetInt() { return i; };
- > };
- >
- > class B : public A
- > {
- > protected:
- > int Foo(A * pA) { return pA->GetInt(); };
- > };
- > ============================================
- >
- >The error I'm getting is on the line declaring B::Foo(): error C2248:
- >'GetInt' : cannot access protected member declared in class 'A'
- >
- >My C++ bible knows the following about the protected keyword: "When
- >preceding a list of class members, the protected keyword specifies that
- >those members are accessible only from member functions and friends of
- >the class and its derived classes."
- > ^^^^^^^^^^^^^^^
-
- But you are not trying to access it from the member of a derived class. You are
- trying to access GetInt() from an object of class A.
-
- >According to this definition, B should have access to A::GetInt().
- >
-
- B does have access to it
-
- B::Foo()
- {
- return GetInt();
- }
-
- for example would compile. I guess what I am confused on is why you are
- passing an pointer to an object of class A into Foo. Remember, since B publicly
- inherits A, you are saying B "is a" A, There is no reason to pass an object of
- type A into Foo, since:
-
- B b;
-
- b.Foo();
-
- b, being on object of class B, is a A also. IOW, you already know which object
- to call GetInt() for, namely b.
-
- >Cheers
- >
- >
- >+-------------------------------------------------------------------+
- >+ Peter Hug mailto://hugp@kp.planet.gen.nz +
- >+ Maxi Solutions Ltd http://kp.planet.gen.nz/users/hugp +
- >+ PO Box 1468 +
- >+ Paraparaumu Beach Phone: +64 4 297-0463 +
- >+ New Zealand Fax: +64 4 297-0465 +
- >+-------------------------------------------------------------------+
- >
-
- I really hope this helps. e-mail me if you don't understand some of my babble,
- and I will try to be more clear.
-
-
- \\\|///
- | ~ ~ |
- (- @ @ -)
- //------------------------oOOo----(_)----oOOo------------------------
- // Ken Sodemann (Stuffle) | Get Warped!!!!
- // Stuffle@PCII.NET |
- // http://users.mwci.net/~stuffle/ |
- //-------------------------------------------------------------------
-
-